<?php
/**
 * Created by PhpStorm.
 * User: asus
 * Date: 2017/8/14
 * Time: 16:50
 */
namespace Wechat\Controller;
use Org\Util\Wechat;
class WechatController extends CommonController
{
    /*配对微信*/
    public function check()
    {
        $options = [
            'token'=>'zsf',
            'appid'=>'wx7440c4b3bc2b9002',
            'appsecret'=>'f72f1328f8204cb78c3020deecbfa976',
        ];
        //获取xml
        $data = $GLOBALS['HTTP_RAW_POST_DATA'];

        $xml = simplexml_load_string($data);
        //别人的openid
        $taopenid = (string)$xml->FromUserName;
        $data2 = (string)$xml->EventKey;
        $myid = str_replace('qrscene_', '', $data2);

        $myuser = M('wechatuser')->where(['id'=>$myid])->find();
        $save = $myuser['tid'].$taopenid.'|';
        if($data2 != NULL){
            M('wechatuser')->where(['id'=>$myid])->save(['tid'=>$save]);
        }
        $weObj = new Wechat($options);
        $weObj->valid();

        $type = $weObj->getRev()->getRevType();
        switch($type) {
            case Wechat::MSGTYPE_TEXT:
                    $weObj->text('欢迎关注【小鸟找房】
这是一个专业，高效，公开，免费的业务转介平台。您可以发布自己的业务信息，也可以随时查看全市经纪人海量的业务转介信息。一键发布，一键呼叫！省时、省心、省力！！')->reply();
                exit;
                break;
            case Wechat::MSGTYPE_EVENT:
                $weObj->text(' 欢迎关注【小鸟找房】
这是一个专业，高效，公开，免费的业务转介平台。您可以发布自己的业务信息，也可以随时查看全市经纪人海量的业务转介信息。一键发布，一键呼叫！省时、省心、省力！！')->reply();
                exit;
                break;
            case Wechat::MSGTYPE_IMAGE:
                $weObj->text("看到了！")->reply();
                break;
            default:
                $weObj->text("联系微信：Anncor")->reply();
        }
    }
    /*创建菜单*/
    public function createmenu()
    {
        $access_token = $this->access_token();
        $url = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.$access_token;
        /*更改后菜单*/
        $data = '
            {
    "button": [
        {
            "type": "view", 
            "name": "经纪人", 
            "url": "http://www.caizhanzhushou.com/index.php?g=Wechat&m=wechat&a=wechatcode&type=8"
        }, 
        {
            "type": "view", 
            "name": "转介商城", 
            "url": "http://www.caizhanzhushou.com/index.php?g=Wechat&m=wechat&a=wechatcode&type=9"
        }, 
        {
            "name": "更多", 
            "sub_button": [
                {
                    "type": "view", 
                    "name": "活动", 
                    "url": "http://149222494.ax.nofollow.51wtp.com/index.php/toupiao/h5/index?vid=149222494&from=groupmessage"
                }, 
                {
                    "type": "view", 
                    "name": "告知", 
                    "url": "http://www.caizhanzhushou.com/index.php?g=Wechat&m=wechat&a=jingjiren"
                },
                {
                    "type": "view", 
                    "name": "租金月付", 
                    "url": "http://www.caizhanzhushou.com/index.php?g=Wechat&m=Mine&a=yuefu"
                },
                {
                    "type": "view", 
                    "name": "客服", 
                    "url": "http://www.caizhanzhushou.com/index.php?g=Wechat&m=wechat&a=kefu"
                },
                {
                    "type": "view", 
                    "name": "APP下载", 
                    "url": "http://www.caizhanzhushou.com/index.php?g=Wechat&m=wechat&a=app"
                },
            ]
        }
    ]
}
        ';
        /*更改前菜单*/
        $data1 = '
            {
    "button": [
        {
            "name": "我是经纪人", 
            "sub_button": [
                {
                    "type": "view", 
                    "name": "经纪人须知", 
                    "url": "http://www.caizhanzhushou.com/index.php?g=Wechat&m=wechat&a=jingjiren"
                }, 
                {
                    "type": "view", 
                    "name": "发布转介", 
                    "url": "http://www.caizhanzhushou.com/index.php?g=Wechat&m=wechat&a=wechatcode&type=1"
                },
                {
                    "type": "view", 
                    "name": "查看转介", 
                    "url": "http://www.caizhanzhushou.com/index.php?g=Wechat&m=Zhuan&a=chakan"
                },
                {
                    "type": "view", 
                    "name": "平台抢单", 
                    "url": "http://www.caizhanzhushou.com/index.php?g=Wechat&m=wechat&a=wechatcode&type=2"
                }
            ]
        }, 
        {
            "name": "房客房东", 
            "sub_button": [
                {
                    "type": "view", 
                    "name": "我要租房", 
                    "url": "http://www.caizhanzhushou.com/index.php?g=Wechat&m=wechat&a=wechatcode&type=6"
                },
                {
                    "type": "view", 
                    "name": "买二手房", 
                    "url": "http://www.caizhanzhushou.com/index.php?g=Wechat&m=wechat&a=wechatcode&type=7"
                }, 
                {
                    "type": "view", 
                    "name": "我是房东", 
                    "url": "http://www.caizhanzhushou.com/index.php?g=Wechat&m=wechat&a=wechatcode&type=3"
                },
                {
                    "type": "view", 
                    "name": "新房楼盘", 
                    "url": "http://www.caizhanzhushou.com/index.php?g=Build&m=show&a=index"
                }
            ]
        }, 
        {
            "name": "更多", 
            "sub_button": [
                {
                    "type": "view", 
                    "name": "个人中心", 
                    "url": "http://www.caizhanzhushou.com/index.php?g=Wechat&m=wechat&a=wechatcode&type=5"
                }, 
                {
                    "type": "view", 
                    "name": "告知", 
                    "url": "http://www.caizhanzhushou.com/index.php?g=Wechat&m=wechat&a=gaozhi"
                },
                {
                    "type": "view", 
                    "name": "往期", 
                    "url": "http://www.caizhanzhushou.com/index.php?g=Wechat&m=wechat&a=app"
                }, 
                {
                    "type": "view", 
                    "name": "App下载", 
                    "url": "http://www.caizhanzhushou.com/index.php?g=Wechat&m=wechat&a=app"
                },
                {
                    "type": "view", 
                    "name": "客服", 
                    "url": "http://www.caizhanzhushou.com/index.php?g=Wechat&m=wechat&a=kefu"
                },
            ]
        }
    ]
}
        ';
        $re = $this->https($url,$data);
        /*查看结果*/
        var_dump($re);
    }
    /*以下是微信登陆*/
    /*type=1  发布转介
      type=2  平台抢单
      type=3  我是房东
      type=4  查看转介
      type=5  个人中心
      type=6  我要租房
      type=7  买二手房

      type=8  新版发布
      type=9  转介商城
      type=10  我的分享
    */
    /*获取微信code*/
    public function wechatcode()
    {
        $type = i('get.type');
        $reurl = 'http://'.$_SERVER['HTTP_HOST'].'/index.php?g=Wechat&m=Wechat&a=wechatuser&type='.$type;
        $redirecturl= urlencode($reurl);
        $appid='wx7440c4b3bc2b9002';
        $url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$redirecturl.'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
        header("Location:$url");
    }

    /*获取微信用户信息*/
    public function wechatuser()
    {
        /*得到type*/
        $type = i('get.type');
        /*得到code*/
        $code= $_GET['code'];
        $appid= 'wx7440c4b3bc2b9002';
        $appsecret='f72f1328f8204cb78c3020deecbfa976';

        $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code";

        $jsoninfo = $this->https($url);
        /*获取到access_token*/
        $access_token=$jsoninfo['access_token'];
        /*得到用户的openid*/
        $openid = $jsoninfo['openid'];

        $urll = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
        /*得到用户的信息*/
        $jsoninfo = $this->https($urll);

        $nickname=$jsoninfo['nickname'];
        $openid = $jsoninfo['openid'];
        $headimg = $jsoninfo['headimgurl'];

        $_SESSION['wechatopenid']=$openid;
        $_SESSION['wechatimg']=$headimg;
        $_SESSION['wechatname']=$nickname;

        $data = [
            'name'=>$nickname,
            'openid'=>$openid,
            'headimg'=>$headimg,
            'isjingjiren'=>0,
            'ischeck'=>0,
            'leijizujin'=>0.00,
            'fanhuanzujin'=>0.00,
            'iszhuce'=>0,
            'isvip'=>0,
            'send'=>0,
            'time'=>date('Y年m月d日 h:i:sa',time()),
        ];
        $save = [
            'name'=>$nickname,
            'headimg'=>$headimg,
            'time'=>date('Y年m月d日 h:i:sa',time()),
        ];
        if(M('wechatuser')->where(['openid'=>$openid])->find()){
            M('wechatuser')->where(['openid'=>$openid])->save($save);
            /*查看转介*/
            if($type==4){
                $this->redirect('Wechat/Zhuan/chakan');
            }
            /*个人中心*/
            if($type==5){
                $this->redirect('Wechat/Mine/index');
            }
            /*发布转介*/
            if($type==1){
                $this->redirect('Wechat/Zhuan/index');
            }
            /*平台抢单*/
            if($type==2){
                $this->redirect('Wechat/Idface/qiangdan');
            }
            /*我是房东*/
            if($type==3){
                $this->redirect('Wechat/Fangdong/index');
            }
            /*我要租房*/
            if($type==6){
                $this->redirect('Wechat/Zufang/index');
            }
            /*买二手房*/
            if($type==7){
                $this->redirect('Wechat/Ershou/index');
            }
            /*新版WEB APP */
            if($type==8){
                $this->redirect('Wechat/Webapp/index');
            }
            /*转介商城*/
            if($type==9){
                $this->redirect('Wechat/Webzhuan/qita');
            }
            /*我的分享*/
            if($type==10){
                $this->redirect('Wechat/Mine/fenxiang');
            }
        }else{
            if(isset($openid)){
                M('wechatuser')->add($data);
            }
            /*查看转介*/
            if($type==4){
                $this->redirect('Wechat/Zhuan/chakan');
            }
            /*个人中心*/
            if($type==5){
                $this->redirect('Wechat/Mine/index');
            }
            /*发布转介*/
            if($type==1){
                $this->redirect('Wechat/Zhuan/index');
            }
            /*平台抢单*/
            if($type==2){
                $this->redirect('Wechat/Idface/qiangdan');
            }
            /*我是房东*/
            if($type==3){
                $this->redirect('Wechat/Fangdong/index');
            }
            /*我要租房*/
            if($type==6){
                $this->redirect('Wechat/Zufang/index');
            }
            /*买二手房*/
            if($type==7){
                $this->redirect('Wechat/Ershou/index');
            }
            /*新版WEB APP */
            if($type==8){
                $this->redirect('Wechat/Webapp/index');
            }
            /*转介商城*/
            if($type==9){
                $this->redirect('Wechat/Webzhuan/qita');
            }
            /*我的分享*/
            if($type==10){
                $this->redirect('Wechat/Mine/fenxiang');
            }
        }
    }
    /*微信自定义分享
     * @$reurl为 传入控制器的完整url地址
     */
    public function zidingfenxiang($reurl)
    {
        $access_token = $this->access_token();
        $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$access_token.'&type=jsapi';
        $data = $this->https($url);

        $_SESSION['ticket'] = $data['ticket'];

        /*随机字符串*/
        $str = $this->randomm();

        /*时间戳*/
        $time = time();

        $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";

        $reurl = "$protocol$reurl";

        $wxOri = sprintf("jsapi_ticket=%s&noncestr=%s&timestamp=%s&url=%s",
            $_SESSION['ticket'], $str, $time,$reurl
        );
        /*得到signature*/
        $wxSha1 = sha1($wxOri);

        $this->assign([
            'time'=>$time,
            'noncestr'=>$str,
            'signature'=>$wxSha1,
            'url'=>$reurl,
        ]);
    }
    /*APP展示页*/
    public function app()
    {
            $url = $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
            $this->zidingfenxiang($url);
            $this->display();
    }
    /*白皮书*/
    public function baipi()
    {
        $url = $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
        $this->zidingfenxiang($url);

        $this->display();
    }
    /*小鸟优势*/
    public function youshi()
    {
        $url = $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
        $this->zidingfenxiang($url);
        $this->display();
    }
    /*我是经纪人*/
    public function jingjiren()
    {
        $url = $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
        $this->zidingfenxiang($url);
        $this->display();
    }
    /*我是客户*/
    public function kehu()
    {
        $url = $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
        $this->zidingfenxiang($url);
        $this->display();
    }
    /*租聘*/
    public function zupin()
    {
        $url = $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
        $this->zidingfenxiang($url);
        $this->display();
    }
    /*二手房*/
    public function ershoufang()
    {
        $url = $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
        $this->zidingfenxiang($url);
        $this->display();
    }
    /*新房*/
    public function xinfang()
    {
        $url = $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
        $this->zidingfenxiang($url);
        $this->display();
    }
    /*经纪人等级说明*/
    public function jingjilv()
    {
        $url = $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
        $this->zidingfenxiang($url);
        $this->display();
    }
    /*成为会员*/
    public function vip()
    {
        $url = $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
        $this->zidingfenxiang($url);
        $this->display();
    }
    /*我是房东*/
    public function fangdong()
    {
        $url = $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
        $this->zidingfenxiang($url);
        $this->display();
    }
    /*告知*/
    public function gaozhi()
    {
        $url = $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
        $this->zidingfenxiang($url);
        $this->display();
    }
    /*客服*/
    public function kefu()
    {
        $url = $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
        $this->zidingfenxiang($url);
        $this->display();
    }
    /*协议*/
    public function xieyi()
    {
        $url = $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
        $this->zidingfenxiang($url);
        $this->display();
    }

}